﻿#include "file.h"
#include <locale.h>
#include "facility.h"
#include "record.h"
#include "facility_manager.h"
#include "personal_info_m.h"

// 读取场地文件
void read_facility_file(System* system, const char* filename) {
	FILE* file = fopen(filename, "r");
	if (!file) {
		printf("Fail to open facility file\n");
		return;
	}
	else {
		// 清空场地链表
		clear_facility(&system->facility_link);
		// 读取文件内容 写入链表中
		int id,type,area,manager_id,max_time_num;
		float price;
		char name[MAX_NAME_LENGTH], open_time[MAX_NAME_LENGTH];

		while (fscanf_s(file, "%d,%[^,],%d,%d,%d,%[^,],%f,%d\n",
			&id,name,MAX_NAME_LENGTH,&manager_id,&type,&area,open_time, MAX_NAME_LENGTH,&price,&max_time_num)==8) {
			Facility facility = create_facility(*system, id, name, manager_id, type, area, open_time, price,max_time_num);
			add_facility(&system->facility_link, &facility);
		}
	}
	fclose(file);
}
// 写入场地文件
void write_facility_file(System system, const char* filename) {
	FILE* file = fopen(filename, "w");
	if (!file) {
		printf("Fail to open facility file");
		return;
	}
	else {
		FNode* cur_node = system.facility_link.head->next;
		while (cur_node != NULL) {
			fprintf(file, "%d,%s,%d,%d,%d,%s,%.2f,%d\n",
				cur_node->facility.id,
				cur_node->facility.name,
				cur_node->facility.manager_id,
				cur_node->facility.type,
				cur_node->facility.area,
				cur_node->facility.open_time,
				cur_node->facility.price_per_half_hour,
				cur_node->facility.max_person_num);
			cur_node = cur_node->next;
		}
	}
	fclose(file);
}
// 读取场地负责人文件
void read_facility_manager_file(System* system,const char* filename) {
	FILE* file = fopen(filename, "r");
	if (!file) {
		printf("Fail to open facility manager file\n");
		return;
	}
	else {
		int id, num_facilities;
		char name[MAX_NAME_LENGTH];
		Facility* facilities[MAX_FACILITIES];
		char username[MAX_USERNAME];
		char password[MAX_PASSWORD];
		long long phone;
		while (fscanf_s(file, "%d,%[^,],%lld,%[^,],%[^,],%d\n",
			&id, name,MAX_NAME_LENGTH, &phone, username,MAX_USERNAME, password,MAX_PASSWORD, &num_facilities) == 6) {
			int facilities_id;
			for (int i = 0; i < num_facilities; i++) {
				fscanf_s(file, "%d", &facilities_id);
				facilities[i] = find_facility(&system->facility_link, facilities_id);
			}
			FacilityManager facility_manager = create_facility_manager(id, name, num_facilities, facilities, username, password, phone);
			add_facility_manager(&system->facility_manager_link, &facility_manager);
		}
	}
}
// 写入场地负责人文件
void write_facility_manager_file(System system, const char* filename) {
	FILE* file = fopen(filename, "w");
	if (!file) {
		printf("Fail to open facility manager file");
		return;
	}
	else {
		FMNode* cur_node = system.facility_manager_link.head->next;
		while (cur_node != NULL) {
			fprintf(file, "%d,%s,%lld,%s,%s,%d\n",
				cur_node->facility_manager.id,
				cur_node->facility_manager.name,
				cur_node->facility_manager.phone,
				cur_node->facility_manager.username,
				cur_node->facility_manager.password,
				cur_node->facility_manager.num_facilities);
			int flag = 0;
			for (int i = 0; i < cur_node->facility_manager.num_facilities; i++) {
				if (flag == 0) {
					fprintf(file, "%d", cur_node->facility_manager.facilities[i]->id);
					flag = 1;
				}
				else  fprintf(file, " %d", cur_node->facility_manager.facilities[i]->id);
			}
			fprintf(file,"\n");
			cur_node = cur_node->next;
		}
	}
	fclose(file);
}
// 读取使用记录
void read_record_file(System* system, const char* filename) {
	FILE* file = fopen(filename, "r");
	if (!file) {
		printf("Fail to open record file\n");
		return;
	}
	else {
		char phone[50];
		int facility_id;
		int int_is_end = 0;
		bool is_end = false;
		int id;
		char year[5], month[3], day[3],begin_time[6],end_time[6];
		int cnt = fscanf_s(file, "%d,%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%d,%d\n",
			&id,phone, 50, year, 5, month, 3, day, 3, begin_time, 6, end_time, 6, &facility_id,&int_is_end);
		if (int_is_end == 1)	is_end = true;
		while (cnt == 9) {

			Facility* facility = find_facility(&system->facility_link, facility_id);
			/*User_node* user_node = find_phone_data(system->user_link, phone);
			User* user = &user_node->user;*/
			User* user = find_user_by_phone(&system->user_link,phone);
			Record record = create_record(user, facility, year, month, day, begin_time, end_time,is_end);
			add_record(&system->record_link, &record);
			cnt = fscanf_s(file, "%d,%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%d,%d\n",
				&id, phone, 50, year, 5, month, 3, day, 3, begin_time, 6, end_time, 6, &facility_id,&int_is_end);
		}
	}
}
// 写入用户文件
void write_record_file(System* system, const char* filename) {
	FILE* file = fopen(filename, "w");
	if (!file) {
		printf("Fail to open record file");
		return;
	}
	else {
		RENode* cur_node = system->record_link.head->next;
		while (cur_node != NULL) {
			fprintf(file, "%d,%s,%s,%s,%s,%s,%s,%d,%d\n",
				cur_node->record.record_id,
				cur_node->record.user->phone,
				cur_node->record.year,
				cur_node->record.month,
				cur_node->record.day,
				cur_node->record.begin_time,
				cur_node->record.end_time,
				cur_node->record.facility->id,
				cur_node->record.is_end);
				cur_node = cur_node->next;
		}
	}
	fclose(file);
}
